import com.alibaba.fastjson.JSON;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.File;
import java.io.FileWriter;
import java.io.Writer;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by huangyifeng on 2019/3/6.
 */
public class Application {

    public static void main(String[] args) throws Exception {
        final List<Address> list = new ArrayList<Address>();
        final JsoupUtil jsoupUtil = new JsoupUtil();
        final PinYinUtil pinYinUtil = new PinYinUtil();
        Document document = jsoupUtil.jsoupConnect("http://www.stats.gov.cn/tjsj/tjbz/tjyqhdmhcxhfdm/2018/index.html", 0);

        final Elements provinceUrls = document.select("tr.provincetr > td > a");
        final String nowDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());

        final List<Thread> threadList = new ArrayList<Thread>();

        // 省级
        for (int i = 0; i < provinceUrls.size(); i++) {
            Thread.sleep(10);
            final Element provinceEle = provinceUrls.get(i);
            Thread thread = new Thread(new Runnable() {
                public void run() {
                    String prohref = provinceEle.attr("abs:href");

                    Address province = new Address();
                    String[] split = prohref.split("/");
                    province.setId(split[split.length - 1].split("\\.")[0]);
                    province.setAddressName(provinceEle.text());
                    province.setLevelVal(1);
                    province.setParentId("0");
                    province.setStatus(0);
                    province.setCreateTime(nowDate);
                    province.setPykey(pinYinUtil.changeToGetShortPinYin(province.getAddressName()).toUpperCase());
                    list.add(province);

                    Document document2 = jsoupUtil.jsoupConnect(prohref, 0);

                    Elements cityEles = document2.select("tr.citytr");

                    // 市级
                    for (int j = 0; j < cityEles.size(); j++) {
                        Element cityEle = cityEles.get(j);
                        Elements cityA = cityEle.select("td > a");
                        String cityName = cityA.get(1).text();

                        Address city = new Address();

                        if (!cityName.equals("县")) {
                            city.setAddressName(cityName);

                            if (cityName.equals("市辖区")) {
                                city.setAddressName(province.getAddressName());
                            }
                            city.setId(cityA.first().text());
                            city.setPykey(pinYinUtil.changeToGetShortPinYin(city.getAddressName()).toUpperCase());
                            city.setLevelVal(2);
                            city.setStatus(0);
                            city.setParentId(province.getId());
                            city.setCreateTime(nowDate);
                            list.add(city);
                        }

                        String cityhref = cityA.attr("abs:href");

                        Document document3 = jsoupUtil.jsoupConnect(cityhref, 0);

                        // 区级
                        Elements countyEles = document3.select("tr.countytr");
                        for (int k = 0; k < countyEles.size(); k++) {
                            Element countyEle = countyEles.get(k);
                            Elements tdEle = countyEle.select("td");

                            Address county = new Address();
                            county.setId(tdEle.first().text());
                            county.setAddressName(tdEle.get(1).text());
                            county.setPykey(pinYinUtil.changeToGetShortPinYin(county.getAddressName()).toUpperCase());
                            county.setLevelVal(3);
                            county.setStatus(0);
                            county.setParentId(city.getId());

                            if (cityName.equals("县")) {
                                county.setParentId("500100000000");
                            }
                            county.setCreateTime(nowDate);
                            list.add(county);

                            Element countyAEle = tdEle.select("a").first();
                            if (countyAEle == null) {
                                continue;
                            }
                            String countyhref = countyAEle.attr("abs:href");

                            Document document4 = jsoupUtil.jsoupConnect(countyhref, 0);

                            // 街道级别
                            Elements townEles = document4.select("tr.towntr");
                            for (int l = 0; l < townEles.size(); l++) {
                                Element townEle = townEles.get(l);
                                Elements townTdEle = townEle.select("td");

                                Address town = new Address();
                                town.setId(townTdEle.first().text());
                                town.setAddressName(townTdEle.get(1).text());
                                town.setPykey(pinYinUtil.changeToGetShortPinYin(town.getAddressName()).toUpperCase());
                                town.setLevelVal(4);
                                town.setStatus(0);
                                town.setParentId(county.getId());
                                town.setCreateTime(nowDate);
                                list.add(town);
                            }
                        }
                    }
                }
            });

            thread.start();
            threadList.add(thread);
        }

        for(int i = 0; i < threadList.size(); i++) {
            threadList.get(i).join();
        }

        System.out.println("size：" + list.size());
        // 创建文件，用于保存数据
        String path = "D:/data.json";
        File datafile = new File(path);
        if (datafile.exists()) {
            datafile.delete();
        }
        datafile.createNewFile();

        // 将数据写入文件
        Writer out =new FileWriter(datafile);
        out.write(JSON.toJSONString(list));
        out.close();
    }
}
